import { ref, computed, nextTick, reactive, onMounted, h } from "vue";
import { type PaginationProps } from "@pureadmin/table";
import { dateFormat } from "@pureadmin/utils";
import { ElDatePicker, ElMessage, ElMessageBox, FormInstance } from "element-plus";
import { emitter } from "@/utils/mitt";
import {
  SecuritiesPortfolioGetAnalysisReport,
  SecuritiesPortfolioGetPurchasedPortfolioData
} from "@/api/stu/Cecurity";
import { useRoute } from "vue-router";
export function usePortfolioRebalancings() {
  const route = useRoute()
  const dataList = ref();
  const dataLists = ref([])
  const Ttimes = ref("")
  const loading = ref<boolean>(true);
  const btnloading = ref<boolean>(false);
  const TimesVisible = ref(false)
  const pagination = reactive<PaginationProps>({
    total: 0,
    pageSize: 10,
    currentPage: 1,
    pageSizes: [5, 10, 15],
    background: true
  });
  const columns: TableColumnList = [
    {
      label: "序号",
      type: "index",
      minWidth: 80
    }, {
      label: "投资组合名称",
      prop: "portfolioldName"
    }, {
      label: "客群名称",
      prop: "clientBaseName",
    }, {
      label: "模拟年化收益率(%)",
      prop: "annualYield"
    }, {
      label: "持仓收益率(%)",
      prop: "positionYield"
    }, {
      label: "模拟年化波动率(%)",
      prop: "annualizedVolatility"
    }, {
      label: "持仓波动率(%)",
      prop: "positionVolatility"
    },
    {
      label: "购买日期",
      prop: "purchaseTime"
    }, {
      label: "分析日期",
      prop: "parsingTime"
    },
    {
      label: "调仓提示",
      fixed: "right",
      width: 120,
      slot: "operations"
    },
    {
      label: "操作",
      fixed: "right",
      width: 250,
      slot: "operation"
    }
  ];
  const isportfolioldId = ref<any>("")
  const DialogVisible = ref(false);
  const OptimizeVisible = ref(false);
  const PropData = reactive({
    data: [],
    portfolioldId: ""
  })

  // 优化及看报告
  async function optimizeReport(row, nums) {
    let res = null;

    if (nums === 1) {
      OptimizeVisible.value = true;
      nextTick(() => {
        PropData.data = JSON.parse(row.fundJson);
        PropData.portfolioldId = row.portfolioldId;
      })
    }
    else if (nums === 0) {
      const data = {
        "planId": route.query.planId,
        "taskId": route.query.taskId,
        portfolioId: row.portfolioldId,
        operationType: 1,
        analyseTime: null,
        type: "投资者"
      };
      res = await SecuritiesPortfolioGetAnalysisReport(data);
      if (res.success) {
        DialogVisible.value = true;
        dataLists.value = [res?.data]
        GetDataList()
      } else {
        ElMessage.error(res.msg);
      }
    } else if (nums === 2) {
      // disabledDate(row.parsingTime)
      isportfolioldId.value = row
      TimesVisible.value = true
    }

  }

  // 投资组合分析
  async function suTimes() {
    const data = {
      "planId": route.query.planId,
      "taskId": route.query.taskId,
      portfolioId: isportfolioldId.value.portfolioldId,
      operationType: 0,
      analyseTime: Ttimes.value,
      type: "投资者"
    };
    btnloading.value = true;
    const res = await SecuritiesPortfolioGetAnalysisReport(data);
    if (res.success) {
      ElMessage.success(res.msg);
      btnloading.value = false;
      GetDataList()
      closesuTimes()
    } else {
      setTimeout(() => { btnloading.value = false; }, 1500)
      ElMessage.error(res.msg);
    }
  }

  function closesuTimes() {
    isportfolioldId.value = ""
    Ttimes.value = ""
    TimesVisible.value = false
  }

  emitter.on("updatePortList", () => {
    OptimizeVisible.value = false;
    GetDataList()
  })

  // 更新优化列表
  emitter.on("updatePropData", (row) => {
    PropData.data = row
  })

  function handleSizeChange() { GetDataList() }
  function handleCurrentChange() { GetDataList() }
  async function GetDataList() {
    const data = {
      pageIndex: pagination.currentPage,
      pageCount: pagination.pageSize,
      taskId: route.query.taskId,
      planId: route.query.planId,
    };
    const res = await SecuritiesPortfolioGetPurchasedPortfolioData(data);
    if (res.success) {
      dataList.value = res.data.items;
      pagination.total = res.data.totalCount;
      loading.value = false;
    } else {
      ElMessage.error(res.msg);
      setTimeout(() => { loading.value = false; }, 1500)
    }
  }

  const disabledDate = (time: Date) => {
    return time.getTime() < new Date(isportfolioldId.value.purchaseTime).getTime()
  }


  onMounted(() => {
    GetDataList()
  })
  return {
    Ttimes,
    dataList,
    loading,
    dataLists,
    pagination,
    columns,
    PropData,
    btnloading,
    TimesVisible,
    DialogVisible,
    OptimizeVisible,
    suTimes,
    disabledDate,
    closesuTimes,
    optimizeReport,
    handleSizeChange,
    handleCurrentChange
  }
}
